Want to take programming class with me this summer. Well I am learning C and have decided to put up my notes on each chapter read. I am a week and a half into the class, so I will add chapters 1 day at a time, till I get caught up so as not to overwhelm you. The notes are from A First Book of Ansi C third edition. by Gary J Bronson. We basically read the book, watch some videos, take part in threaded instructions, and turn in code, (with two test during the semester. If you are using the RSS Feed come to the site (http://dr_bob.blogspot.com/) to take a look. Any way, have fun you all.
Excuse the numbering the HTML is from Microsoft Word wich does not do a great job on HTML code.
Chapter 1 - Getting Started
- Introduction to Programming
A computer is a machine. Like any other machine it needs to be told what to do and that is called a program. All programs do basically the same thing, input data, manipulate the data and output the data. Why are there so many programming languages? There are so many because each has it own specialty niche.
- Science and engineering: Fortran
- Business Applications: COBOL
- Teaching programming: Basic and Pascal
- Structured programming: C
In order to write a program a programmer has to know how to write a step by step sequence of instructions to perform a computation. This is known as an algorithm. You must decide on one way to solve the problem. To simplify this process, begin by writing out the steps in plain English. This is referred to as pseudo code. Math equations are called formulas. Instead of pseudo code a pictorial representations is sometimes used. This is referred to as a flow chart.
Our next step would be to change the algorithm into a program (coding). Once coded it needs to be converted to machine language so the computer can use it (translated). In C we use a compiler to covert all the code to an executable file. Other programming languages use an interpreter and convert one line if code at a time as needed.
- Introduction to Modularity
Just as a building does not happen, neither do programs. They require planning. The structure is the program's overall construction. In constructing modular programming we will need to use modules. Each has a specific task in taking in, processing, and outputting data. In C, these modules are called functions.
Functions should be given good name (identifiers) that reflects what they really do. They need to follow the following rules:
- First character MUST be letter or underscore (_)
- Second and all following must be alphanumeric or underscore (_)
- Cannot be a reserve word
- No more than 31 characters (compilers may vary on this)
Each C program has a function named main where all other functions are called. It, as all functions start with a function header line. This tells the following information:
- What type of data is returned
- Name of the function
- Type of data, if any, is sent into the function
Here is an example:
int main()
in this example, int is integer, the type of value that will be returned, main is the name of the function, and () has the information that is being sent into the program. Since the ( ) are empty, there is no information being sent in. Any information (arguments) sent in would be in-between the ( ). After this will follow curly braces {} with the program between them. All statements insode the {} must have a semicolon at the end (;).
1.3 The printf() function
To put output ot a screen, the printf() function needs to be used. Anything that is between the ( ) will be output to the screen. It should be in " " in the ( ).
Comments are important to all programs as they tell you information on what was done, and why. They can be added to your code by putting them in-between /* and */. All comments will not compile when it comes time to compile the program. Do not nest one set of comments inside another set.
To use the printf() function, you will need to add a preprocessor line. This will include pre-built functions. For the printf() function we will use stdio.h. It is added before the main() function like this:
#include <stdio.h>
It is called a header file because of its locations and does not need to include a ; at the end.
printf() uses the \ character to do special things. One of these is \n which does a new line. The \ is called an escape character.
- Programming Style
While there is no specific way to write a program it is a good idea to make it readable. Lines should be indented as needed to express clarity. The compiler will ignore most white-space (space between characters). Also one should use comments liberally to help you and others what was being done in the program.
- Top Down Development
Five steps in program development
- Determine what the output is that the program needs to produce
- Determine what the inputs are to be.
- Design the program
- Select the algorithm for transferring the input to the output
- Check algorithm by hand with specific values
- Code in C.
- Test with selected data.
- Common Programming Errors
Steps 1 & 2 are the analysis phase.
Step 3 is the design phase.
Step 4 is the coding phase.
Step 5 is the testing phase.
- Omitting the () after the main function
- Omitting or not typing correctly the { at the start of the program
- Omitting or not typing correctly the } at the start of the program
- Misspelling a function name
- Forgetting to close the message in a printf() function with a "
- Omitting a semicolon (;) at the end of a statement
- Forgetting the \n to indicate a new line.
One major mistake that all new programmers make is that the start to code before they know what the program is supposed to do, what algorithms are needed, and a clear understanding of what is to be done. Take time from the start and you will get it right.